using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace DrivingGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class SplashScreen : Microsoft.Xna.Framework.DrawableGameComponent
    {
        protected Game1 game;
        MouseState currentMouse, previousMouse;
        SpriteBatch spriteBatch;
        private Texture2D splashBackground;
        private Texture2D startButtonText, settingButtonText, exitButtonText, highScoreButton;
        private Texture2D startButtonTextS, settingButtonTextS, exitButtonTextS, highScoreButtonS;
        private Vector2 btnSize;
        List<Menu> buttons = new List<Menu>();

        private bool MouseMoved
        {
            get
            {
                return currentMouse.X != previousMouse.X || currentMouse.Y != previousMouse.Y;
            }
        }
        private bool MouseLeftClick
        {
            get
            {
                return previousMouse.LeftButton == ButtonState.Pressed &&
                   currentMouse.LeftButton == ButtonState.Released;
            }
        }

        public SplashScreen(Game1 game, ContentManager Content)
            : base(game)
        {
            this.game = game;
            splashBackground = Content.Load<Texture2D>("Background//splash");
            btnSize = new Vector2(150, 100);
            Vector2 middle = new Vector2(game.GraphicsDevice.Viewport.Width / 2, game.GraphicsDevice.Viewport.Height / 2);
            
            loadTextures();
            buttons.Add(new Menu(startButtonText, startButtonTextS, "START", new Vector2(10, middle.Y)));
            buttons.Add(new Menu(highScoreButton, highScoreButtonS, "HIGHSCORE", new Vector2(210, middle.Y)));
            buttons.Add(new Menu(settingButtonText, settingButtonTextS, "SETTING", new Vector2(410 , middle.Y)));
            buttons.Add(new Menu(exitButtonText, exitButtonTextS, "EXIT", new Vector2(610, middle.Y)));

            game.IsMouseVisible = true;
            previousMouse = currentMouse = Mouse.GetState();
        }

        private void loadTextures()
        {
            startButtonText = game.Content.Load<Texture2D>("Button/start-active");
            startButtonTextS = game.Content.Load<Texture2D>("Button/start-selected");
            highScoreButton = game.Content.Load<Texture2D>("Button/highscore-active");
            highScoreButtonS = game.Content.Load<Texture2D>("Button/highscore-selected");
            settingButtonText = game.Content.Load<Texture2D>("Button/setting-active");
            settingButtonTextS = game.Content.Load<Texture2D>("Button/setting-selected");
            exitButtonText = game.Content.Load<Texture2D>("Button/exit-active");
            exitButtonTextS = game.Content.Load<Texture2D>("Button/exit-selected");
        }


        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            previousMouse = currentMouse;
            currentMouse = Mouse.GetState();
            if (MouseMoved)
            {
                foreach (Menu btn in buttons)
                {
                    btn.Selected = btn.Bounds.Contains(currentMouse.X, currentMouse.Y);
                }
            }
            if (MouseLeftClick)
            {
                Menu clicked = null;
                foreach (Menu btn in buttons)
                    if (btn.Bounds.Contains(currentMouse.X, currentMouse.Y))
                    {
                        clicked = btn;
                        break;
                    }
                if (clicked != null)
                {
                    switch (clicked.name)
                    {
                        case "START":
                            game.IsMouseVisible = false;
                            game.GameState = "START";
                            break;
                        case "SETTING":
                            break;
                        case "EXIT":
                            game.Exit();
                            break;
                    }
                }
            }

        }

        public void DrawSplash()
        {
            spriteBatch.Begin();

            drawSplashBackground();
            drawMenu();

            spriteBatch.End();
        }

        private void drawSplashBackground()
        {
            int screenWidth = GraphicsDevice.PresentationParameters.BackBufferWidth;
            int screenHeight = GraphicsDevice.PresentationParameters.BackBufferHeight;
            Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);
            spriteBatch.Draw(splashBackground, screenRectangle, Color.White);
        }

        public void drawMenu()
        {
            foreach (Menu btn in buttons)
                if (btn.Selected)
                    spriteBatch.Draw(btn.selectedTexture, btn.position, Color.White);
                else
                    spriteBatch.Draw(btn.texture, btn.position, Color.White);
        }
    }
}
